//
//  ScrollWithArrayView.m
//  product_B
//
//  Created by lanou on 16/5/28.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ScrollWithArrayView.h"
#import "HomePageMainModel.h"
#import "SmallScrollView.h"

@implementation ScrollWithArrayView

- (instancetype)initWithFrame:(CGRect)frame modelArray:(NSMutableArray *)modelArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollArray = [NSMutableArray array];
        self.sizeArray = [NSMutableArray array];
        self.modelArray = modelArray;
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.scrollView.contentSize = CGSizeMake(frame.size.width * modelArray.count+5, 0);
        self.scrollView.bounces = YES;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.pagingEnabled = YES;
        self.scrollView.delegate = self;
        [self addSubview:_scrollView];
        for (int i = 0; i < modelArray.count; i++) {
            UIScrollView *smallScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(i*frame.size.width+10, 5, frame.size.width-20, frame.size.height)];
            smallScroll.contentSize = CGSizeMake(frame.size.width-20, frame.size.height+100);
            smallScroll.bounces = YES;
            smallScroll.backgroundColor = [UIColor clearColor];
            smallScroll.showsVerticalScrollIndicator = NO;
            smallScroll.showsHorizontalScrollIndicator = NO;
            smallScroll.pagingEnabled = YES;
            smallScroll.userInteractionEnabled = YES;
            smallScroll.delegate = self;
            [self.scrollView addSubview:smallScroll];
            [_scrollArray addObject:_scrollView];
            SmallScrollView *smallView = [[UINib nibWithNibName:@"SmallScrollView" bundle:nil] instantiateWithOwner:self options:nil].firstObject;
            smallView.frame = CGRectMake(5, 5, frame.size.width-30, frame.size.height-70);
            smallView.layer.shadowOffset = CGSizeMake(2, 2);
            smallView.layer.shadowRadius = 5;
            smallView.layer.shadowOpacity = 0.5;
            smallView.layer.shadowColor = [UIColor blackColor].CGColor;
            smallView.layer.cornerRadius = 5;
            smallView.userInteractionEnabled = YES;
            [smallScroll addSubview:smallView];
            HomePageMainModel *model = modelArray[i];
            [smallView.picImageView sd_setImageWithURL:[NSURL URLWithString:model.hp_img_original_url]];
            smallView.titleLabel.text = model.hp_title;
            smallView.authorLabel.text = model.hp_author;
            smallView.contentLabel.text = model.hp_content;
            CGFloat height = model.hp_content.length /( (kWidth-50)/17) * 21;
            smallView.frame = CGRectMake(5, 5, kWidth - 30, 10 + smallView.picImageView.frame.size.height + 13 + smallView.titleLabel.frame.size.height + 20 + height + kHeight / 667 * 50);
            if (smallView.frame.size.height > frame.size.height) {
                [smallScroll setContentSize:CGSizeMake(0, 10 + smallView.picImageView.frame.size.height + 13 + smallView.titleLabel.frame.size.height + 20 + height + 20 +  150 )];
            }
            smallView.size = smallView.frame.size.height - smallScroll.contentOffset.y;
            [_sizeArray addObject:[NSString stringWithFormat:@"%f",smallView.size]];
            if (_size==0) {
                _size = smallView.frame.size.height - smallScroll.contentOffset.y;
            }

        }
    }
    return self;
}

- (void)begin:(float)size{
    if (_scrollDelegate && [_scrollDelegate respondsToSelector:@selector(clickedWithContentOffset:)]) {
        [_scrollDelegate clickedWithContentOffset:size];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{


    if (_scrollDelegate && [_scrollDelegate respondsToSelector:@selector(clickedWithContentOffset:)]) {
        [_scrollDelegate clickedWithContentOffset:scrollView.subviews[0].frame.size.height - scrollView.contentOffset.y];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
